package com.loubobooo.personal.blog.service.impl;

import com.loubobooo.personal.blog.entity.vo.UserVO;
import com.loubobooo.personal.blog.mapper.UserMapper;
import com.loubobooo.personal.blog.mapper.custom.UserMapperCustom;
import com.loubobooo.personal.blog.entity.User;
import com.loubobooo.personal.blog.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 用户管理service
 * @author: loubobooo
 * @date: 2018/3/23 15:46
 */
@Service public class UserServiceImpl implements UserService {
    @Autowired private UserMapperCustom userMapperCustom;

    @Autowired private UserMapper userMapper;

    @Override public List<UserVO> listUser() {
        List<UserVO> userVOList = userMapperCustom.listUser();
        for (int i = 0; i < userVOList.size(); i++) {
            Integer articleCount = userMapperCustom.countArticleByUser(userVOList.get(i).getUserId());
            userVOList.get(i).setArticleCount(articleCount);
        }
        return userVOList;
    }

    @Override public UserVO getUserById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    @Override public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override public void deleteUser(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override public void insertUser(User user) {
        user.setUserRegisterTime(new Date());
        userMapper.insertSelective(user);
    }

    public User login(String username, String password){
        return userMapperCustom.selectLogin(username, password);
    }

    @Override public User getUserByNameOrEmail(String name, String email) {
        return userMapperCustom.getUserByNameOrEmail(name, email);
    }

    @Override public Integer getCountByNameOrEmail(String name, String email) {
        return userMapperCustom.getCountByNameOrEmail(name, email);
    }


}
